package com.handsome.单例模式;

/**
 * 单例模式（枚举）
 * 1.借助JDK1.5中添加的枚举来实现单例模式，不仅能避免多线程同步问题，而且能防止反序列化重新创建新的对象
 * 2.这种方式是effective java作者约书亚推荐的方式
 * 3.推荐使用
 * @author niechun
 * @date 2023/1/8 10:20
 */
public class SingletonEnum {
    public static void main(String[] args) {
        System.out.println(Runtime.getRuntime().totalMemory()/(1024*1024));
        SingletonEnumTest instance = SingletonEnumTest.INSTANCE;
        SingletonEnumTest instance2 = SingletonEnumTest.INSTANCE;
        System.out.println(instance==instance2);
        System.out.println(instance.hashCode());
        System.out.println(instance2.hashCode());

    }
}
enum SingletonEnumTest {
    INSTANCE;
public void print(){
    System.out.println("ok");
}

}
enum SeasonEnum {
    SPRING("spring"),
    SUMMER("summer"),
    AUTUMN("autumn"),
    WINTER("winter");

    private final String name;
    SeasonEnum(String name){
        this.name=name;
    }
    public void getSeason(){
        System.out.println(name);
    }

}